import { createRouter, createWebHashHistory } from "vue-router"
import clockRoutes from "./clock"
import knowledgeRoutes from "./knowledge"
import timelineRoutes from "./timeline"
import messageRoutes from "./message"
import mineRoutes from "./mine"
import userRoutes from "./user"

import store from "@/store/index"

const routes = [
  {
    path: "/",
    name: "Index",
    redirect: "/mine",
    component: () =>
      import(/* webpackChunkName: "about" */ "@/views/index.vue"),
    children: [
      ...clockRoutes,
      ...knowledgeRoutes,
      ...timelineRoutes,
      ...messageRoutes,
      ...mineRoutes
    ]
  },
  ...userRoutes
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const token = store.getters["user/token"]
  const userRole = store.getters["user/userRole"]
  console.log(userRole,to);
  if(userRole==='visitor' && to.path==='/home') {
    next({ path: "/knowledge" })
  } else if (token || userRole==='visitor' ||to.path === "/login") {
    next()
  } else {
    next({ path: "/login" })
  }

  // if(to.meta.requireAuth){
  // 	if(store.state.userId){
  // 		next()
  // 	}else{
  // 		next({path:'/b'})
  // 	}
  // }else{
  // 	next()
  // }
})

export default router
